﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using Client.p2p;

namespace Client
{
    public partial class UpdateUserDataForm : Form
    {
        public UpdateUserDataForm()
        {
            InitializeComponent();
        }

        public UpdateUserDataForm(p2p.GlobalCmdBackground globalCmdBg)
        {
            this.globalCmdBg = globalCmdBg;
            this.globalCmdBg.CommandReceived += new CommandReceivedEventHandler(Receiver);
            InitializeComponent();
            this.skinEngine.SkinFile = "DeepCyan.ssk";
        }

        private DateTime latestFileTime;
        private byte[] latestFileContent = null;
        private int receivedCount = 0;

        private void Receiver(object sender, CommandEventArgs e)
        {
            Command cmd = e.Command;
            switch (cmd.CommandType)
            {
                case p2p.CommandType.QueryLatestUserDataAck:
                    int timelen = BitConverter.ToInt32(cmd.MetaData, 0);
                    DateTime time = DateTime.Parse(Encoding.ASCII.GetString(cmd.MetaData, 4, timelen));
                    if (latestFileTime == null || latestFileTime < time)
                    {
                        latestFileTime = time;
                        int filelen = BitConverter.ToInt32(cmd.MetaData, 4 + timelen);
                        latestFileContent = new byte[filelen];
                        Array.Copy(cmd.MetaData, 4 + timelen + 4, latestFileContent, 0, latestFileContent.Length);
                    }
//                     this.globalCmdBg.SendCommand(new Command(p2p.CommandType.TcpClose,
//                         cmd.TcpClient,
//                         "QueryLatestUserData",
//                         null));
                    receivedCount++;
                    this.labelTip.Text = "接收到" + this.receivedCount.ToString() + "个用户数据";
                    this.btnCancel.Text = "确定";
                    break;
            }
        }

        private p2p.GlobalCmdBackground globalCmdBg;

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Stop();
            if (latestFileContent != null)
            {
	            File.WriteAllBytes("UserData.db", latestFileContent);
	            File.SetLastWriteTime("UserData.db", latestFileTime);
                this.globalCmdBg.CommandReceived -= new CommandReceivedEventHandler(Receiver);
                this.Close();
            }
        }

        private void UpdateUserDataForm_Shown(object sender, EventArgs e)
        {
            this.globalCmdBg.SendCommand(new p2p.Command(p2p.CommandType.QueryLatestUserData,
                null,
                "Global",
                this.globalCmdBg.MulticastEP,
                Encoding.ASCII.GetBytes(File.GetLastWriteTime("UserData.db").ToString())));
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (latestFileContent == null)
            {
                MessageBox.Show("找不到最新的用户数据，将使用本地数据");
            }
            else
            {
                File.WriteAllBytes("UserData.db", latestFileContent);
                File.SetLastWriteTime("UserData.db", latestFileTime);
            }
            this.globalCmdBg.CommandReceived -= new CommandReceivedEventHandler(Receiver);
            this.Close();
        }
    }
}
